package com.class05.controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@ResponseBody
public class AcceptParamController {

    //    3.5接收请求头的参数 语法格式：利用@RequestHeader(value = "name")来接收参数
    @RequestMapping("/head")
    public List<String> getHead(
            @RequestHeader(value = "host") String host,
            @RequestHeader(value = "accept-language") String language,
            @RequestHeader(value = "accept-encoding", required = false) String encoding,
            @RequestHeader(value = "Connection", required = false) String connection
    ) {
        List<String> objects = new ArrayList<>();
        objects.add(host);
        objects.add(language);
        objects.add(encoding);
        objects.add(connection);
        return objects;

    }


//    3.6原生API接收请求头的参数：请求域、会话域、应用域、响应域
    @RequestMapping("/domain")
    public List<Map> getDoMain(HttpServletRequest request,
                               HttpServletResponse response,
                               HttpSession session

                               ) {
        List<Map> list = new ArrayList<>();
        Map<String, Object> requestMap = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            requestMap.put(headerName, headerValue);
        }
        list.add(requestMap);
        String id = session.getId();
        Map<String, Object> sessionMap = new HashMap<>();
        sessionMap.put("sessionId", id);
        list.add(sessionMap);
        return list;



    }


//    3.7springmvc中Request级别属性（共享）域：Map、Model、ModelMap、ModelAndView

    @RequestMapping("/model")
    public String getModel(Map map) {
        map.put("name", "zhangsan");
        return "hello";
    }
    @RequestMapping("/model1")
    public String getModel1(Model model) {
        model.addAttribute("name", "zhangsan");
        return "hello";
    }
    @RequestMapping("/model2")
    public String getModel2(ModelMap modelMap) {
        modelMap.addAttribute("name", "zhangsan");
        return "hello";
    }
    @RequestMapping("/model3")
    public String getModel3(ModelAndView modelAndView) {
        modelAndView.addObject("name", "zhangsan");
        return "hello";
    }

}
